package com.bnyy.medicalHousekeeper.activity;

import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.blankj.utilcode.util.ImageUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.SizeUtils;
import com.blankj.utilcode.util.SpanUtils;
import com.bnyy.gbp.customsView.ScaleImageView;
import com.bnyy.gbp.permission.PermissionHelper;
import com.bnyy.medicalHousekeeper.GlideHelper;
import com.bnyy.medicalHousekeeper.PopupWindowHelper;
import com.bnyy.medicalHousekeeper.R;
import com.bnyy.medicalHousekeeper.base.BaseActivityImpl;
import com.bnyy.medicalHousekeeper.bean.CustomerDetail;
import com.bnyy.medicalHousekeeper.bean.Goods;
import com.bnyy.medicalHousekeeper.bean.ShareData;
import com.bnyy.medicalHousekeeper.moudle.message.ChatManager;
import com.bnyy.medicalHousekeeper.moudle.message.activity.ChatMessageActivity;
import com.bnyy.medicalHousekeeper.moudle.message.activity.FocusAndFansActivity;
import com.bnyy.medicalHousekeeper.moudle.message.activity.Share2ContactsActivity;
import com.bnyy.medicalHousekeeper.request.BaseObserverImpl;
import com.bnyy.medicalHousekeeper.request.RequestManager;
import com.bnyy.message.bean.ContactUser;
import com.bnyy.message.bean.chat.message_data.UserHomePageMessage;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

import butterknife.BindView;
import cn.bingoogolapple.qrcode.zxing.QRCodeEncoder;

/**
 * Author : 关炳鹏
 * Description :
 * Date : 2023/2/9
 **/
public class CustomerDetailActivity extends BaseActivityImpl {
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_sex)
    TextView tvSex;
    @BindView(R.id.tv_age)
    TextView tvAge;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_vip_level)
    TextView tvVipLevel;
    @BindView(R.id.tv_check_detail)
    TextView tvCheckDetail;
    @BindView(R.id.tv_no_health_data)
    TextView tvNoHealthData;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.hs)
    HorizontalScrollView hs;
    @BindView(R.id.tv_check_more)
    TextView tvCheckMore;
    @BindView(R.id.tv_no_purchase_record)
    TextView tvNoPurchaseRecord;
    @BindView(R.id.tv_purchase_time)
    TextView tvPurchaseTime;
    @BindView(R.id.tv_goods_name)
    TextView tvGoodsName;
    @BindView(R.id.ll_goods)
    LinearLayout llGoods;
    @BindView(R.id.ll_other_info)
    LinearLayout llOtherInfo;
    @BindView(R.id.tv_send_message)
    TextView tvSendMessage;
    @BindView(R.id.iv_header)
    ScaleImageView ivHeader;
    @BindView(R.id.iv_goods)
    ScaleImageView ivGoods;

    private int userId;

    public static void show(Context context, int id) {
        Intent intent = new Intent(context, CustomerDetailActivity.class);
        intent.putExtra("id", id);
        context.startActivity(intent);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_customer_detail;
    }

    @Override
    public String getTitleStr() {
        return "用户详情";
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        userId = getIntent().getIntExtra("id", -1);

        HashMap<String, Object> params = new HashMap<>();
        params.put("id", userId);
        requestManager.request(requestManager.mJavaRetrofitService.getCustomerDetail(RequestManager.getJsonRequestBody(params)),
                new BaseObserverImpl<CustomerDetail>() {
                    @Override
                    public void onSuccess(CustomerDetail customerDetail) {
                        super.onSuccess(customerDetail);

                        GlideHelper.setCircleImage(mContext, customerDetail.getUser_image(), ivHeader, R.mipmap.icon_default_header_man);
                        tvName.setText(customerDetail.getUser_name());
                        tvSex.setText(customerDetail.getSex());
                        tvAge.setText(customerDetail.getAge() + "岁");
                        tvPhone.setText("联系方式：" + customerDetail.getPhone());
                        tvVipLevel.setText("用户等级：" + customerDetail.getMember_level_desc());

                        tvSendMessage.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                HashMap<String, Object> params = new HashMap<>();
                                params.put("id", userId);
                                params.put("role_id", customerDetail.getRoleId());
                                requestManager.request(requestManager.mMessageRetrofitService.getUserHomepage(RequestManager.getJsonRequestBody(params)),
                                        new BaseObserverImpl<ContactUser>(mContext) {
                                            @Override
                                            public void onSuccess(ContactUser contactUser) {
                                                super.onSuccess(contactUser);
                                                ChatMessageActivity.show(mContext, contactUser);
                                            }
                                        });

                            }
                        });

                        ArrayList<CustomerDetail.HealthData> healthDatas = customerDetail.getHealthData();
                        if (healthDatas != null && healthDatas.size() > 0) {
                            tvNoHealthData.setVisibility(View.GONE);

                            hs.setVisibility(View.VISIBLE);
                            tvTime.setVisibility(View.VISIBLE);
                            tvTime.setText(customerDetail.getTestDate());
                            tvCheckDetail.setVisibility(View.VISIBLE);
                            tvCheckDetail.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                }
                            });
                            int dp12 = SizeUtils.dp2px(12);

                            LinearLayout llRoot = new LinearLayout(mContext);
                            llRoot.setOrientation(LinearLayout.HORIZONTAL);
                            hs.addView(llRoot);

                            for (int i = 0; i < healthDatas.size(); i++) {
                                CustomerDetail.HealthData healthData = healthDatas.get(i);

                                LinearLayout linearLayout = new LinearLayout(mContext);
                                linearLayout.setOrientation(LinearLayout.VERTICAL);
                                FrameLayout.LayoutParams layoutParams = new HorizontalScrollView.LayoutParams(ScreenUtils.getScreenWidth() / 2, ViewGroup.LayoutParams.WRAP_CONTENT);

                                if (i != healthDatas.size() - 1) {
                                    layoutParams.setMarginEnd(dp12);
                                }
                                linearLayout.setLayoutParams(layoutParams);

                                linearLayout.setPadding(dp12, dp12, dp12, dp12);

                                GradientDrawable bg = new GradientDrawable();
                                CustomerDetail.HealthData.Detail value = healthData.getValue();
                                bg.setColor(Color.parseColor(value.getBackground_color()));
                                bg.setCornerRadius(dp12);

                                linearLayout.setBackground(bg);

                                TextView tvType = new TextView(mContext);
                                tvType.setTextColor(Color.parseColor(value.getFont_color()));
                                tvType.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
                                tvType.getPaint().setFakeBoldText(true);
                                tvType.setText(healthData.getType_desc());
                                tvType.setPadding(0, 0, 0, dp12);

                                Drawable drawable = null;
                                String unit = "";
                                switch (healthData.getType()) {
                                    case 0:
                                        drawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_heart_rate);
                                        unit = " 次/分钟";
                                        break;
                                    case 3:
                                        drawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_blood_pressure);
                                        unit = " mmHg";
                                        break;
                                    case 4:
                                        drawable = ContextCompat.getDrawable(mContext, R.mipmap.icon_blood_oxygen);
                                        unit = " %";
                                        break;
                                }

                                if (drawable != null) {
                                    drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                                    tvType.setCompoundDrawables(drawable, null, null, null);
                                    tvType.setCompoundDrawablePadding(dp12 / 2);
                                }
                                linearLayout.addView(tvType);

                                TextView tvValue = new TextView(mContext);
                                tvValue.setTextColor(Color.parseColor(value.getValue_color()));
                                SpanUtils spanUtils = SpanUtils.with(tvValue)
                                        .append(value.getValue()).setFontSize(16, true).setBold();

                                if (!TextUtils.isEmpty(unit)) {
                                    spanUtils.append(unit);
                                }
                                spanUtils.create();
                                linearLayout.addView(tvValue);

                                linearLayout.setOnClickListener(new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        HealthDataDetailActivity.show(mContext, healthData.getWear_id(), healthData.getType());
                                    }
                                });

                                llRoot.addView(linearLayout);
                            }

                        }

                        ArrayList<Goods> purchaseRecords = customerDetail.getPurchaseRecord();
                        if (purchaseRecords != null && purchaseRecords.size() > 0) {
                            tvNoPurchaseRecord.setVisibility(View.GONE);
                            tvCheckMore.setVisibility(View.VISIBLE);
                            tvCheckMore.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    OrderActivity.show(mContext, customerDetail.getId());
                                }
                            });
                            tvPurchaseTime.setVisibility(View.VISIBLE);
                            tvPurchaseTime.setText(customerDetail.getPurchaseTime());
                            llGoods.setVisibility(View.VISIBLE);

                            Goods goods = purchaseRecords.get(0);
                            GlideHelper.setRoundImage(mContext, goods.getGoods_image(), ivGoods);
                            tvGoodsName.setText(goods.getGoods_name());
                        }

                        ArrayList<CustomerDetail.OtherInfo> otherInfos = customerDetail.getOtherInfo();
                        if (otherInfos != null && otherInfos.size() > 0) {
                            llOtherInfo.setVisibility(View.VISIBLE);

                            for (int i = 0; i < otherInfos.size(); i++) {
                                CustomerDetail.OtherInfo otherInfo = otherInfos.get(i);

                                LinearLayout linearLayout = new LinearLayout(mContext);
                                linearLayout.setGravity(Gravity.CENTER_VERTICAL);
                                linearLayout.setOrientation(LinearLayout.HORIZONTAL);

                                TextView tvType = new TextView(mContext);
                                tvType.setTextAppearance(R.style.NormalTextView);
                                tvType.setTextColor(ContextCompat.getColor(mContext, R.color.gray_light));
                                tvType.setText(otherInfo.getType_desc());
                                tvType.setPadding(0, 0, SizeUtils.dp2px(12), 0);

                                linearLayout.addView(tvType);

                                TextView tvTime = new TextView(mContext);
                                tvTime.setGravity(Gravity.END);
                                tvTime.setTextAppearance(R.style.NormalTextView);
                                tvTime.setText(otherInfo.getDate());
                                tvTime.setSingleLine(true);
                                tvTime.setEllipsize(TextUtils.TruncateAt.END);

                                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT);
                                layoutParams.weight = 1;
                                linearLayout.addView(tvTime, layoutParams);

                                llOtherInfo.addView(linearLayout);
                                if (i != otherInfos.size() - 1) {
                                    inflater.inflate(R.layout.divider_with_margin, llOtherInfo, true);
                                }
                            }
                        }
                    }
                });

    }
}




























































